/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Parents;

import Address.AddressBean;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author mishra
 */
public class EditParents extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        int i = 0;
        ParentsBean pb = new ParentsBean();
        AddressBean adb = new AddressBean();
        try {
            if (request.getParameter("admissionId").length() != 0) {
                pb.setAdmissionId(Integer.parseInt(request.getParameter("admissionId")));
            }
            if (request.getParameter("parid").length() != 0) {
                pb.setParentId(Integer.parseInt(request.getParameter("parid")));
            }
            int chkParrent = Parents.Query.chkParents(pb.getAdmissionId(), pb.getParentId());

            adb.setAddressId(Integer.parseInt(request.getParameter("paddid")));
            adb.setAddress1(request.getParameter("add1"));
            adb.setAddress2(request.getParameter("add2"));
            adb.setCountryId(Integer.parseInt(request.getParameter("country")));
            adb.setStateId(Integer.parseInt(request.getParameter("state")));
            adb.setPinCode(request.getParameter("pin"));
            if (chkParrent != 0) {
                Address.Query.edit(adb);
            } else {
                adb.setAddressId(Address.Query.add(adb));
            }

            pb.setAddressId(adb.getAddressId());

            pb.setFatherName(request.getParameter("fName"));
            pb.setFatherMobileNo(request.getParameter("fMobile"));
            pb.setMotherName(request.getParameter("mName"));
            pb.setMotherMobileNo(request.getParameter("mMobile"));
            pb.setFatherOccupation(request.getParameter("fOccupation"));
            pb.setMotherOccupation(request.getParameter("mOccupation"));
            pb.setGuardianName(request.getParameter("gName"));
            pb.setRelation(request.getParameter("relation"));
            pb.setGuardianOccupation(request.getParameter("gOccupation"));
            pb.setGuardianMobileNo(request.getParameter("gMobile"));

            pb.setEntryEmail(session.getAttribute("uid").toString());
            if (chkParrent != 0) {
                i = Parents.Query.edit(pb);
            } else {
                i = Parents.Query.add(pb);
            }
            if (i != 0) {
                session.setAttribute("status", "Success");
                session.setAttribute("msg", "Parrents/Guardian Details Edited Successfully.");
                response.sendRedirect("/RIT/Services/Admission/Admission.jsp");
            } else {
                session.setAttribute("status", "Fail");
                session.setAttribute("msg", "Check Details and try again.");
                response.sendRedirect("/RIT/Services/Admission/Admission.jsp");
            }
        } catch (Exception e) {
            session.setAttribute("status", "Fail");
            session.setAttribute("msg", "Check Details and try again (e).");
            response.sendRedirect("/RIT/Services/Admission/Admission.jsp");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
